package cn.accp.mvc.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import cn.accp.mvc.dao.HouseDao;
import cn.accp.mvc.entity.House;
import cn.accp.mvc.entity.Page;
import cn.accp.mvc.entity.allEntity;

@Repository("houseDao")
public class HouseDaoImpl extends SqlSessionDaoSupport implements HouseDao {
	@Autowired
	@Override
	public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory) {
		// TODO Auto-generated method stub
		super.setSqlSessionFactory(sqlSessionFactory);
	}
	@Autowired
	private Page page;
	public void setPage(Page page) {
		this.page = page;
	}
	@Override
	public boolean save(House house) {
		boolean isOK = false;
		int iRet = getSqlSession().insert("insertHouse", house);
		if(iRet != 0){
			isOK=true;
		}
		return isOK;
	}

	@Override
	public List<allEntity> findHouseByPage(int pageIndex, int pageSize) {
		List<allEntity> housesList = new ArrayList<allEntity>();
		housesList = page.findHouseByPage(pageIndex, pageSize);
		return housesList;
	}

	@Override
	public int houseCount(int pageSize) {
		int iRet = 0;
		iRet = getSqlSession().selectOne("houseTotal");
		iRet = iRet%pageSize==0?iRet/pageSize:iRet/pageSize+1;
		return iRet;
	}

	@Override
	public boolean delete(int id) {
		boolean isOK = false;
		int iRet = getSqlSession().delete("deleteHouse",id);
		if(iRet != 0){
			isOK = true;
		}
		return isOK;
	}

	@Override
	public allEntity getHouse(int id) {
		allEntity Entity = new allEntity();
		Entity = getSqlSession().selectOne("findAllEntityById",id);
		return Entity;
	}

	@Override
	public boolean update(House house) {
		boolean isOK = false;
		int iRet = getSqlSession().update("updateHouse", house);
		if(iRet != 0){
			isOK=true;
		}
		return isOK;
	}

}
